function* gen() {
    // yield  同步异步都行
    yield 1
    yield Promise.resolve(2)
    yield Promise.reject(3)
    // yield setTimeout("ai",0)
    yield console.log('end')
}

let g = gen()
console.log(g.next())
console.log(g.next())
console.log(g.next())
console.log(g.next())
console.log(g.next())
console.log(g.next())


// for of 原理
const each = (value: any) => {
    let It = value[Symbol.iterator]()     //拿到迭代器
    let next: any = { done: false }
    while (!next.done) {
        next = It.next()
        if (!next.done) {
            console.log(next.value)
        }
    }
}
each([1, 2, 3])
console.log('------------')
let object = {
    max: 5,
    current: 0,
    [Symbol.iterator]() {
        return {
            max: this.max,
            current: this.current,
            next() {
                if (this.current == this.max) {
                    return {
                        value: undefined,
                        done: true
                    }
                } else {
                    return {
                        value: this.current++,
                        done: false
                    }
                }
            }
        }
    }
}

for (let value of object) {
    console.log(value)
}

for (let key of Object.keys(object)) {
    console.log(key,object[key])
}

